package cn.rengy.web.framework.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import cn.rengy.lang.ResultEntity;
import cn.rengy.tool.core.util.ExceptionPrintUtils;
import cn.rengy.web.framework.sql.DbService;
import lombok.extern.slf4j.Slf4j;


/**
 * 简单请求
 * 
 */
@RestController
@Slf4j
public class SimpleAccessController {
	
	@Autowired
	private DbService dbService;
	/**
	 * 执行一个查询sql(?参数)，返回List
	 * 登录用户可访问
	 * @param sqlid
	 * @param param
	 * @return
	 */
	@RequestMapping(value="/authenticated/simple/list",method = RequestMethod.POST)
	public ResultEntity<?> authSimpleQuery(@RequestParam("id") String sqlid,@RequestBody(required=false) Object[] param){
		return ResultEntity.ok(dbService.queryForList(sqlid, param));
	}
	@RequestMapping(value="/simple/list",method = RequestMethod.POST)
	public ResultEntity<?> simpleQuery(@RequestParam("id") String sqlid,@RequestBody(required=false) Object[] param){
		return ResultEntity.ok(dbService.queryForList(sqlid, param));
	}
	/**
	 * 执行一个查询sql(具名参数)，返回List
	 * 登录用户可访问
	 * @param sqlid
	 * @param param
	 * @return
	 */
	@RequestMapping(value="/authenticated/simple/list/named",method = RequestMethod.POST)
	public ResultEntity<?> simpleNamedQuery(@RequestParam("id") String sqlid,@RequestBody(required=false) Map<String,Object> paramMap){
		return ResultEntity.ok(dbService.queryForListMapParameter(sqlid, paramMap==null?new HashMap<String,Object>():paramMap));
	}
	/**
	 * 执行一个查询sql(?参数)，返回List
	 * 登录用户可访问
	 * @param sqlid
	 * @param param
	 * @return
	 */
	@RequestMapping(value="/authenticated/simple/map",method = RequestMethod.POST)
	public ResultEntity<?> authAimpleQueryForMap(@RequestParam("id") String sqlid,@RequestBody(required=false) Object[] param){
		return ResultEntity.ok(dbService.queryForMap(sqlid, param));
	}
	@RequestMapping(value="/simple/map",method = RequestMethod.POST)
	public ResultEntity<?> simpleQueryForMap(@RequestParam("id") String sqlid,@RequestBody(required=false) Object[] param){
		return ResultEntity.ok(dbService.queryForMap(sqlid, param));
	}
	@RequestMapping(value="/authenticated/ajax/body/{id}",method = RequestMethod.POST)
	public List<Map<String,Object>> responseBody (@PathVariable("id") String sqlid,@RequestBody(required=false) Map<String,Object> paramMap){
		return dbService.queryForListMapParameter(sqlid,paramMap==null?new HashMap<String,Object>():paramMap);
	}
	
	@RequestMapping(value="/authenticated/ajax/{id}",method = RequestMethod.POST)
	public ResultEntity<?> query (@PathVariable("id") String sqlid,@RequestBody(required=false) Map<String,Object> paramMap){
		return ResultEntity.ok(dbService.queryForListMapParameter(sqlid, paramMap==null?new HashMap<String,Object>():paramMap));
	}
	
	@RequestMapping(value="/authenticated/simple/update/{id}",method = RequestMethod.POST)
	public ResultEntity<?> update (@PathVariable("id") String sqlid,@RequestBody(required=false) Map<String,Object> paramMap){
		return ResultEntity.ok(dbService.updateMapParameter(sqlid, paramMap));
	}
	@RequestMapping(value="/simple/update/{id}",method = RequestMethod.POST)
	public ResultEntity<?> simpleUpdate (@PathVariable("id") String sqlid,@RequestBody(required=false) Map<String,Object> paramMap){
		return ResultEntity.ok(dbService.updateMapParameter(sqlid, paramMap));
	}
	@RequestMapping(value="/authenticated/ajax/insert/{id}",method = RequestMethod.POST)
	public ResultEntity<?> insert (@PathVariable("id") String sqlid,@RequestBody Map<String,Object> paramMap){
		return ResultEntity.ok(dbService.updateReturnGeneratedKeys(sqlid, paramMap));
	}
	/**
	 * 执行一组sql update语句
	 * @param sqlid
	 * @param paramMap
	 * @return
	 */
	@RequestMapping(value="/authenticated/ajax/updates/{id}",method = RequestMethod.POST)
	public ResultEntity<?> updates (@PathVariable("id") String sqlid,@RequestBody Map<String,Object> paramMap){
		return ResultEntity.ok(dbService.updates(sqlid, paramMap));
	}
	/**
	 * 查询单个对象返回map
	 * @param sqlid
	 * @param paramMap
	 * @return
	 */
	@RequestMapping(value="/authenticated/ajax/map/{id}",method = RequestMethod.POST)
	public ResultEntity<?> queryForMap(@PathVariable("id") String sqlid,@RequestBody(required=false) Map<String,Object> paramMap){
		return ResultEntity.ok(dbService.queryForMapParameter(sqlid, paramMap==null?new HashMap<String,Object>():paramMap));
	}
	/**
	 * 查询返回布尔值
	 * @param sqlid
	 * @param paramMap
	 * @return
	 */
	@RequestMapping(value="/authenticated/ajax/boolean/{id}",method = RequestMethod.POST)
	public Boolean getBoolean (@PathVariable("id") String sqlid,@RequestBody Map<String,Object> paramMap){
		int count=dbService.querySingleColumn(sqlid, paramMap, Integer.class);
		return (count==0?true:false);
	}
	/**
	 * 查询多个sql集合返回
	 * @param sqlid
	 * @param paramMap
	 * @return
	 */
	@RequestMapping(value="/authenticated/ajax/multiple",method = RequestMethod.POST)
	public ResultEntity<?> queryBysqlids(@RequestBody Map<String,Object> paramMap){
		return null;
	}
	
	
}
